package zhtt.repositoryrest;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.format.annotation.DateTimeFormat;
import zhtt.entity.User;

import java.util.Date;
import java.util.List;

/**
 * Created by ztt on 2018/3/18.
 */
@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepositoryRest extends JpaRepository<User, Integer> {

    /**
     * 根据用户名查询
     * @param username
     * @return
     */
    @RestResource(path = "findByUsername", rel = "findByUsername")
    List<User> findByUsername(@Param("username") String username);

    /**
     * 自定义sql查询
     * @param username
     * @return
     */
    @RestResource(path = "likeByUsername", rel = "likeByUsername")
    @Query("select u from User u where u.username like '%'|| :username || '%' ")
    List<User> likeByUsername(@Param("username") String username);

    /**
     * 分页查询
     * @param pageable
     * @return
     */
    @RestResource(path = "page", rel = "page")
    List<User> findByUsernameNotNull(Pageable pageable);//search/page?page=0&size=2

    @RestResource(path = "between", rel = "between")
    List<User> findByCreateTimeBetween(@DateTimeFormat(pattern = "yyyyMMddHH") @Param("startTime") Date startTime, @DateTimeFormat(pattern = "yyyyMMddHH") @Param("endTime") Date endTime);//http://localhost:8080/user/search/between?startTime=20170101000000&endTime=20190101000000
}
